<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="utf-8">
  <title>Sign in &laquo; Admin</title>
  <link rel="stylesheet" href="../assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="../assets/css/admin.css">
</head>

<body>
  <div class="login">
    <div class="login-wrap">
      <img class="avatar" src="../assets/img/default.png">
      <!-- 有错误信息时展示 -->
      <!-- <div class="alert alert-danger">
        <strong>错误！</strong> 用户名或密码错误！
      </div> -->
      <div class="form-group">
        <label for="email" class="sr-only">邮箱</label>
        <input id="email" type="email" class="form-control" placeholder="邮箱" autofocus>
      </div>
      <div class="form-group">
        <label for="password" class="sr-only">密码</label>
        <input id="password" type="password" class="form-control" placeholder="密码">
      </div>
      <button class="btn btn-primary btn-block" id="btn">登 录</button>
    </div>
  </div>
  <script src="../assets/vendors/jquery/jquery.min.js"></script>
  <script>
    $(function () {
      var loginBtn = $('#btn')
      //登陆按钮事件
      loginBtn.on('click', function () {
        var email = $('#email').val()
        var password = $('#password').val()
        // 登陆非空验证
        if (email.trim().length == 0) {
          alert('请输入邮箱地址');
          return;
        }
        if (password.trim().length == 0) {
          alert('请输入密码');
          return;
        }
        //能执行到此处说明通过了验证
        $.ajax({
          url: '/login',
          type: 'post',
          data: {
            email,
            password,
          },
          success: function (data) {

            //对用户的角色进行判断，管理员跳转到管理页面，普通用户网站首页
            if (data.role == 'admin') {  //跳转到index页面
              location.href = '/admin/index.html'
            }
            else {
              location.href = '/index.html'
            }


          },
          error: function () {
            //alert
            alert('用户名或密码错误')
          }
        })

      })
    })
  </script>
</body>

</html>